<template>
  <div>
    <div class="nologin-box margin-auto" v-if="!userInfo">
      <p>登录网易云音乐，可以享受无限收藏的乐趣，并且无限同步到手机</p>
      <button class="login-button" @click="isloginDialog">用户登录</button>
    </div>
    <div
      class="nologin-box islogin-box"
      v-else
      v-loading="isLoginBox"
      :element-loading-background="ELEMENT_LOADING_BACKGROUND"
      element-loading-spinner="el-icon-loading"
      :element-loading-text="ELEMENT_LOADING_TEXT"
    >
      <div class="user-info">
        <div class="user-img-box" @click="routerScreen('myHomepage')">
          <img v-if="userImgNew" width="100%" :src="userImgNew" alt="">
          <img v-else width="100%" :src="userDetail.profile.avatarUrl" alt="" />
        </div>
        <div class="user-info-detail">
          <div class="name" @click="routerScreen('myHomepage')">
            {{ userDetail.profile.nickname }}
          </div>
          <div class="grade" @click="routerScreen('vipMember')">
            Lv. <i style="font-style: none">{{ userDetail.level }}</i>
          </div>
          <div class="sign">
            <button class="blue-button">签到</button>
          </div>
        </div>
      </div>
      <div class="user-control">
        <router-link
          :to="{ name: 'dynamic', query: { id: userDetail.profile.userId } }"
          tag="div"
        >
          <p class="font-size">{{ userDetail.profile.eventCount }}</p>
          <p>动态</p>
        </router-link>
        <router-link
          :to="{ name: 'attention', query: { id: userDetail.profile.userId } }"
          tag="div"
        >
          <p class="font-size">{{ userDetail.profile.follows }}</p>
          <p>关注</p>
        </router-link>
        <router-link
          :to="{ name: 'fans', query: { id: userDetail.profile.userId } }"
          tag="div"
        >
          <p class="font-size">{{ userDetail.profile.followeds }}</p>
          <p>粉丝</p>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { userDetail } from "@/api";
export default {
  name: "PersonalDetails",
  computed: {
    ...mapGetters(["userInfo","userDetails","userImgNew"]),
  },
  data() {
    return {
      isLoginBox: false,
      userDetail: {
        profile: {},
      },
    };
  },
  watch: {
    userInfo(newVal) {
      if (newVal) {
        this.isLoginBox = true;
        userDetail({ uid: newVal.profile.userId }).then((data) => {
          this.userDetail = data;
          this.isLoginBox = false;
        });
      }
    },
  },
  created() {
    if (this.userInfo) {
      this.isLoginBox = true;
      userDetail({ uid: this.userInfo.profile.userId }).then((data) => {
        this.userDetail = data;
        this.isLoginBox = false;
        this.SET_CHANGE_USER_Details(data)
      });
    }
  },
  methods: {
    ...mapActions("common", ["SET_CHANGE_LOGIN_DIALOG"]),
    ...mapActions("user",["SET_CHANGE_USER_Details"]),
    isloginDialog() {
      this.SET_CHANGE_LOGIN_DIALOG();
    },
    routerScreen(val) {
      this.$router.push({
        name: val,
        query: {
          id: this.userInfo.profile.userId,
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.islogin-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  .user-info {
    margin-top: 8px;
    font-size: 12px;
    display: flex;
    .user-img-box {
      cursor: pointer;
      width: 80px;
      height: 80px;
      margin-right: 20px;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        padding: 3px;
        border: 1px solid #dadada;
        width: 100%;
      }
    }
    .user-info-detail {
      .name:hover {
        text-decoration: underline;
      }
      .name {
        cursor: pointer;
        font-size: 14px;
        color: #333;
        font-weight: 600;
      }
      .grade {
        cursor: pointer;
        font-style: italic;
        color: #999;
        font-weight: 600;
        border: 1px solid #999;
        line-height: 1;
        padding: 2px 7px;
        border-radius: 8px;
        display: inline-block;
      }
      .sign {
        margin-top: 10px;
        width: 90px;
        height: 28px;
        button {
          cursor: pointer;
          width: 100%;
          height: 100%;
        }
      }
    }
  }
  .user-control {
    margin-top: 10px;
    display: flex;
    & > div:nth-last-child(1) {
      border-right: 0;
    }
    & > div:hover {
      cursor: pointer;
      color: #0c73c2;
    }
    & > div {
      text-align: center;
      font-size: 12px;
      color: #666;
      padding: 0 20px;
      border-right: 1px solid #ccc;
      .font-size {
        font-size: 18px;
      }
    }
  }
}
.nologin-box {
  height: 180px;
  box-sizing: border-box;
  background-image: linear-gradient(#fbfbfb, #ddd);
  border: 1px solid #fff;
  padding: 10px 20px;
  font-size: 12px;
  color: #666;
  line-height: 24px;
  outline: 1px solid #b1b1b1;
  .login-button {
    display: block;
    cursor: pointer;
    width: 100px;
    height: 30px;
    border-radius: 3px;
    color: #fff;
    margin: 0 auto;
    background-image: linear-gradient(#ed212a, #cf070e);
    border: 1px solid #bd0d13;
    outline: 0;
    border: 0;
    text-shadow: 0 1px 0 #8a060b;
    margin-top: 15px;
    &:hover {
      background-image: linear-gradient(rgb(247, 61, 72), rgb(213, 24, 31));
    }
  }
}
.margin-auto{
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
</style>